//
//  HomeRequest.m
//  Learn
//
//  Created by cjg on 2018/11/3.
//  Copyright © 2018 cjg. All rights reserved.
//

#import "HomeRequest.h"
#import "TextbookModel.h"
#import "TextbookDetailModel.h"
#import "TextbookChapterModel.h"
#import "TextbookChapterDetailModel.h"
#import "TextbookQuestionsModel.h"
#import "GradeModel.h"
#import "HSTextBookModel.h"
#import "HSTextBookUnitModel.h"
#import "HSTextBookUnitWordModel.h"
#import "HSTextBookWordinfoModel.h"

@implementation HomeRequest

//获取首页列表接口
+(void)getHomeListDataWithParam:(NSDictionary *)param result:(void(^)(HomeListRootModel *listModel))resultBlock {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_HomePage];
    [BaseRequest baseGET:url cache:NO autoHideLoading:NO parms:param request:^(id resultObject, NSError *error) {

        HomeListRootModel *model = [HomeListRootModel modelWithJSON:resultObject];

        resultBlock(model);
    }];
    
}

+(void)getTextbookListWithGrade:(NSString *)grade type:(NSString *)type page:(NSInteger)page result:(void(^)(NSMutableArray <TextbookModel *> *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:grade forKey:@"grade"];
    [param setValue:@(page) forKey:@"page"];
    [param setValue:@(10) forKey:@"pageSize"];
    
    NSString *api = @"";
    if ([type isEqualToString:@"听说"]) {
        api = KM_API_Hearsay;
    } else if ([type isEqualToString:@"听力"]) {
        api = KM_API_Listenling;
    } else if ([type isEqualToString:@"跟读"]) {
        api = KM_API_Repeat;
    } else if ([type isEqualToString:@"笔试"]) {
        api = KM_API_Writing;
    } else if ([type isEqualToString:@"音标"]) {
        api = KM_API_Soundmark;
    } else if ([type isEqualToString:@"典范"]) {
        api = KM_API_Modelsp;
    } else if ([type isEqualToString:@"新概念"]) {
        api = KM_API_Newconcept;
    } else if ([type isEqualToString:@"黑布林"]) {
        api = KM_API_Blackplum;
    }  else {
        
    }
    

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,api];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSArray *arr = resultObject[@"data"];
            resultBlock([TextbookModel mj_objectArrayWithKeyValuesArray:arr]);
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}
+(void)getTextbookListWithKeyWord:(NSString *)keyWord type:(NSString *)type page:(NSInteger)page result:(void(^)(NSMutableArray <TextbookModel *> *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@"" forKey:@"grade"];
    [param setValue:keyWord forKey:@"keyword"];
    [param setValue:@(page) forKey:@"page"];
    [param setValue:@(10) forKey:@"pageSize"];
    
    NSString *api = @"";
    if ([type isEqualToString:@"听说"]) {
        api = KM_API_Hearsay;
    } else if ([type isEqualToString:@"听力"]) {
        api = KM_API_Listenling;
    } else if ([type isEqualToString:@"跟读"]) {
        api = KM_API_Repeat;
    } else if ([type isEqualToString:@"笔试"]) {
        api = KM_API_Writing;
    } else if ([type isEqualToString:@"音标"]) {
        api = KM_API_Soundmark;
    } else if ([type isEqualToString:@"典范"]) {
        api = KM_API_Modelsp;
    } else if ([type isEqualToString:@"新概念"]) {
        api = KM_API_Newconcept;
    } else if ([type isEqualToString:@"黑布林"]) {
        api = KM_API_Blackplum;
    }  else {
        
    }
    

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,api];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSArray *arr = resultObject[@"data"];
            resultBlock([TextbookModel mj_objectArrayWithKeyValuesArray:arr]);
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}
+(void)getTextbookDetailWithProductId:(NSInteger)productId type:(NSString *)type result:(void(^)(TextbookDetailModel *model))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@(productId) forKey:@"id"];
    NSString *api = @"";
    if ([type isEqualToString:@"听说"]) {
        api = KM_API_HearsayDetail;
    } else if ([type isEqualToString:@"听力"]) {
        api = KM_API_ListenlingDetail;
    } else if ([type isEqualToString:@"跟读"]) {
        api = KM_API_RepeatDetail;
    } else if ([type isEqualToString:@"笔试"]) {
        api = KM_API_WritingDetail;
    } else if ([type isEqualToString:@"音标"]) {
        api = KM_API_SoundmarkDetail;
    } else if ([type isEqualToString:@"典范"]) {
        api = KM_API_ModelspDetail;
    } else if ([type isEqualToString:@"新概念"]) {
        api = KM_API_NewconceptDetail;
    } else if ([type isEqualToString:@"黑布林"]) {
        api = KM_API_BlackplumDetail;
    }  else {
        
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,api];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            resultBlock([TextbookDetailModel mj_objectWithKeyValues:dic]);
        } else {
            resultBlock(nil);
        }

    }];
}

+(void)getTextbookChapterListWithProductId:(NSInteger)productId type:(NSString *)type order:(NSString *)order result:(void(^)(NSMutableArray <TextbookChapterModel *> *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@(productId) forKey:@"id"];
    [param setValue:order forKey:@"order"];
    NSString *api = @"";
    if ([type isEqualToString:@"听说"]) {
        api = KM_API_HearsayChapterList;
    } else if ([type isEqualToString:@"听力"]) {
        api = KM_API_ListenlingChapterList;
    } else if ([type isEqualToString:@"跟读"]) {
        api = KM_API_RepeatChapterList;
    } else if ([type isEqualToString:@"笔试"]) {
        api = KM_API_WritingChapterList;
    } else if ([type isEqualToString:@"音标"]) {
        api = KM_API_SoundmarkChapterList;
    } else if ([type isEqualToString:@"典范"]) {
        api = KM_API_ModelspChapterList;
    } else if ([type isEqualToString:@"新概念"]) {
        api = KM_API_NewconceptChapterList;
    } else if ([type isEqualToString:@"黑布林"]) {
        api = KM_API_BlackplumChapterList;
    }  else {
        
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,api];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSArray *arr = resultObject[@"data"];
            resultBlock([TextbookChapterModel mj_objectArrayWithKeyValuesArray:arr]);
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}

+(void)getTextbookChapterDetailWithProductId:(NSInteger)productId type:(NSString *)type chapterid:(NSUInteger)chapterId result:(void(^)(TextbookChapterDetailModel *model))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@(productId) forKey:@"pid"];
    [param setValue:@(chapterId) forKey:@"id"];
    NSString *api = @"";
    if ([type isEqualToString:@"听说"]) {
        api = KM_API_HearsayChapterDetail;
    } else if ([type isEqualToString:@"听力"]) {
        api = KM_API_ListeningChapterDetail;
    } else if ([type isEqualToString:@"跟读"]) {
        api = KM_API_RepeatChapterDetail;
    } else if ([type isEqualToString:@"笔试"]) {
        api = KM_API_WritingChapterDetail;
    } else if ([type isEqualToString:@"音标"]) {
        api = KM_API_SoundmarkChapterDetail;
    } else if ([type isEqualToString:@"典范"]) {
        api = KM_API_ModelspChapterDetail;
    } else if ([type isEqualToString:@"新概念"]) {
        api = KM_API_NewconceptChapterDetail;
    } else if ([type isEqualToString:@"黑布林"]) {
        api = KM_API_BlackplumChapterDetail;
    }  else {
        
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,api];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            resultBlock([TextbookChapterDetailModel mj_objectWithKeyValues:dic]);
        } else {
            resultBlock(nil);
        }
    }];
}

+(void)getTextbookTestQuestionsWithId:(NSString *)testId type:(NSString *)type result:(void(^)(TextbookQuestionsModel *model))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:testId forKey:@"id"];
    NSString *api = @"";
    if ([type isEqualToString:@"听说"]) {
        api = KM_API_HearsayQuestions;
    } else if ([type isEqualToString:@"听力"]) {
        api = KM_API_ListeningQuestions;
    } else if ([type isEqualToString:@"跟读"]) {
        api = KM_API_RepeatQuestions;
    } else if ([type isEqualToString:@"笔试"]) {
        api = KM_API_WritingQuestions;
    } else if ([type isEqualToString:@"音标"]) {
        api = KM_API_SoundmarkQuestions;
    } else if ([type isEqualToString:@"典范"]) {
        api = KM_API_ModelspQuestions;
    } else if ([type isEqualToString:@"新概念"]) {
        api = KM_API_NewconceptQuestions;
    } else if ([type isEqualToString:@"黑布林"]) {
        api = KM_API_BlackplumQuestions;
    }  else {
        
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,api];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            resultBlock([TextbookQuestionsModel mj_objectWithKeyValues:dic]);
        } else {
            resultBlock(nil);
        }
    }];
}

+(void)getGradeListWithResult:(void(^)(NSMutableArray <GradeModel *> *list))resultBlock {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_Grade];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:@{} result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil) {
                NSArray *arr = dic[@"data"];
                resultBlock([GradeModel mj_objectArrayWithKeyValuesArray:arr]);
            } else {
                resultBlock(@[].mutableCopy);
            }
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}

+(void)getTextBookWithResult:(void(^)(NSMutableArray <HSTextBookModel *> *list))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_textbookstudent];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:@{} result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil) {
                NSArray *arr = resultObject[@"data"];
                resultBlock([HSTextBookModel mj_objectArrayWithKeyValuesArray:arr]);
            } else {
                resultBlock(@[].mutableCopy);
            }
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}

+(void)getTextBookUnitWithId:(NSString *)pId Result:(void(^)(NSMutableArray <HSTextBookUnitModel *> *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:pId forKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_TextbookUnit];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil) {
                NSArray *arr = resultObject[@"data"];
                resultBlock([HSTextBookUnitModel mj_objectArrayWithKeyValuesArray:arr]);
            } else {
                resultBlock(@[].mutableCopy);
            }
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}

+(void)getTextBookUnitWorkWithId:(NSString *)pId Result:(void(^)(NSMutableArray <HSTextBookUnitWordModel *> *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:pId forKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_TextbookUnitWord];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil) {
                NSArray *arr = resultObject[@"data"];
                resultBlock([HSTextBookUnitWordModel mj_objectArrayWithKeyValuesArray:arr]);
            } else {
                resultBlock(@[].mutableCopy);
            }
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}

+(void)getTextBookWordInfoWithId:(NSString *)pId moduleId:(NSUInteger)moduleId Result:(void(^)(TextBookUnitSourceListModel *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:pId forKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_TextbookWordinfo];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            TextBookUnitSourceListModel *model = [TextBookUnitSourceListModel modelWithJSON:dic];
            NSArray *arr = dic[@"resource"];
            if (moduleId == 30) {
                model.lessonResource = [TextBookUnitLessonModel mj_objectArrayWithKeyValuesArray:arr];
            } else {
                model.wordResource = [TextBookUnitWordModel mj_objectArrayWithKeyValuesArray:arr];
                if (moduleId == 10) {
                    model.textResource = [TextBookUnitTextModel mj_objectArrayWithKeyValuesArray:arr];
                }
            }
            resultBlock(model);
        } else {
            resultBlock(nil);
        }
    }];
}

+(void)scanBookWithKey:(NSString *)key page:(NSInteger)page result:(void(^)(NSMutableArray <ScanBookModel *> *list))resultBlock {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:key forKey:@"key"];
    [param setValue:@(page) forKey:@"page"];
    [param setValue:@(10) forKey:@"pageSize"];

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_Scan];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil) {
                NSArray *arr = resultObject[@"data"];
                resultBlock([ScanBookModel mj_objectArrayWithKeyValuesArray:arr]);
            } else {
                resultBlock(@[].mutableCopy);
            }
        } else {
            resultBlock(@[].mutableCopy);
        }

    }];
}

+ (void)upLoadAudioWithPath:(NSString *)filePath result:(void(^)(NSDictionary *url))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_Upload];
    [BaseRequest basePOST:url cache:NO audioFile:filePath autoHideLoading:YES parms:@{} result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil && !IsStrEmpty(dic[@"fullurl"])) {
//                NSArray *arr = resultObject[@"data"];
                resultBlock(dic);
            } else {
                resultBlock(@{});
            }
        } else {
            resultBlock(@{});
        }
    }];
}

+ (void)submitTingshuoworkWith:(NSDictionary *)param result:(void(^)(NSDictionary *data))resultBlock {

    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_postSaveExercise];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            NSDictionary *dic = resultObject[@"data"];
            if (dic != nil) {
                resultBlock(dic);
            } else {
                resultBlock(nil);
            }
        } else {
            resultBlock(nil);
        }

    }];
}

// 提交答案接口
+ (void)submitAnserWith:(NSDictionary *)param result:(void(^)(BOOL ret))resultBlock {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,KM_API_SaveMissionAnswer];
    [BaseRequest basePOST:url cache:NO autoHideLoading:YES parms:param result:^(id resultObject, NSError *error) {
        if (resultObject != nil && [resultObject[@"code"] integerValue] == 1) {
            resultBlock(YES);
        } else {
            resultBlock(NO);
        }
    }];

}

@end
